<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HttpUnit Tutorial - create pool editor - step 1</title>
<LINK REL="stylesheet" HREF="tutorial.css" TYPE="text/css">
</head>

<body>
<p class="location"><a href="index.html">Tutorial</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="-&gt;"> <a href="task1.html">Task 1</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="-&gt;"> Step 1: Invoking the pool editor</p>

<h1>Invoking the Pool Editor</h1>
<p class="goals">In this step, you will learn how to:<br />
&bull; Initialize ServletUnit<br />
&bull; Invoke a servlet<br />
&bull; Specify a username and password for basic authentication</p>
<p>The first step will simply be to verify that we can register
and access the servlet, which we will name <code>PoolEditorServlet</code>. A GET method to this page should return the editor form
itself, while updates will be handled by a POST method to the same address.  Since we are working with servlets, we can
bypass the web server and use the <code>servletunit</code> package to run our tests.</p>

<p>Here is the initial test code:</p>
<pre class="test-code">
<b>package</b> tutorial;

<b>import</b> com.meterware.httpunit.*;
<b>import</b> com.meterware.servletunit.*;
<b>import</b> java.util.*;
<b>import</b> junit.framework.*;
<b>import</b> tutorial.persistence.*;

<b>public class</b> PoolEditorTest <b>extends</b> TestCase {

    <b>public static void</b> main( String args[] ) {
        junit.textui.TestRunner.run( suite() );
    }

    <b>public static</b> TestSuite suite() {
        <b>return new</b> TestSuite( PoolEditorTest.<b>class</b> );
    }

    <b>public</b> PoolEditorTest( String s ) {
        <b>super</b>( s );
    }

    <b>public void</b> testGetForm() <b>throws</b> Exception {
        ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );       // (1) use the web.xml file to define mappings
        ServletUnitClient client = sr.newClient();               // (2) create a client to invoke the application

        try {
            client.getResponse( "http://localhost/PoolEditor" ); // (3) invoke the servlet w/o authorization
            fail( "PoolEditor is not protected" );
        } catch (AuthorizationRequiredException e) {             // (4) verify that access is denied
        }

        client.setAuthorization( "aUser", "pool-admin" );        // (5) specify authorization and
        client.getResponse( "http://localhost/PoolEditor" );     //     invoke the servlet again
    }

}
</pre>

<p>This code uses <code>JUnit</code> and <code>ServletUnit</code> to verify that a servlet is present at the specified address.
The significant points in the code are:<ol>
<li>Creating the <code>ServletRunner</code> class which represents access to a Servlet application.
The application is defined by an XML file which maps URL information to servlet classes.</li>
<li>Creating a client which can access the application and maintain state across multiple invocations.</li>
<li>Invoking the servlet via its URL. Note that ServletUnit ignores any host and port information. All URL patterns
are treated as being relative to the root ("/").</li>
<li>Catching an exception which indicates that authentication is required.</li>
<li>Specifying the authorization information. ServletUnit does not maintain a database of users, no any username is
accepted, and the password is interpreted as a comma-separated list of role names associated with the user.</li></ol></p>

<p>To run this code, you will also need the <a href="web.xml">web.xml</a> file in your current directory. This file
maps the request URL to the Pool Editor servlet.</p>

<p>This code should fail with a <code>HttpNotFoundException</code>, because we have not yet created the servlet class. We
can now proceed to do so. Here is a simple implementation:</p>
<p></p>
<pre class="servlet-code">
<b>package</b> tutorial;

<b>import</b> java.io.*;
<b>import</b> java.util.*;

<b>import</b> javax.servlet.http.*;
<b>import</b> javax.servlet.ServletException;

<b>import</b> tutorial.persistence.*;

<b>public class</b> PoolEditorServlet <b>extends</b> HttpServlet {

    <b>protected void</b> doGet( HttpServletRequest request, HttpServletResponse response )
            <b>throws</b> ServletException, IOException {
        response.setContentType( "text/html" );
        PrintWriter pw = response.getWriter();

        pw.println( "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;" );
        printBody( pw );
        pw.println( "&lt;/body&gt;&lt;/html&gt;" );
    }

    <b>private void</b> printBody( PrintWriter pw ) {
        pw.println( "A simple page" );
    }
}
</pre>
<p>With this code in place, the first test will now pass and we can move to <a href="task1editor-form.html">the next task</a>.</p>

</body>
</html>